var aFunc = {
	plusReady: function() {
		if (mui.os.plus) {
			plus.navigator.closeSplashscreen();
			plus.screen.lockOrientation("portrait-primary");
			plus.navigator.setStatusBarBackground(common.colour.statusBackground);
			aVariable.webview.current = plus.webview.currentWebview();
			aVariable.webview.current.setStyle({
				bounce: "vertical",
				bounceBackground: common.colour.pageBackground,
				bounceBackground: common.colour.pageBackground,
			});
		}
		plus.webview.currentWebview().setStyle({
			bounce: "vertical",
			bounceBackground: common.colour.pageBackground,
		});
		aFunc.bindEvent();
		aFunc.initData();

		var height = document.documentElement.clientHeight || document.body.clientHeight;
		plus.webview.currentWebview().setStyle({
			height: height
		});
		var originalHeight = document.documentElement.clientHeight || document.body.clientHeight;
		window.onresize = function() {
			//软键盘弹起与隐藏  都会引起窗口的高度发生变化
			var resizeHeight = document.documentElement.clientHeight || document.body.clientHeight;
			if (resizeHeight * 1 < originalHeight * 1) { //resizeHeight<originalHeight证明窗口被挤压了
				plus.webview.currentWebview().setStyle({
					height: originalHeight,
					softinputMode: "adjustResize"
				});
			}
		}

	},


	initData: function() {
		var self = aVariable.webview.current;
		aVariable.data.classId = self.classId;
		aVariable.data.signId = self.signId;
		aVariable.data.remarkId = self.remarkId;
		aFunc.requsetRemarkType();


	},

	bindEvent: function() {
		aVariable.btn.startDate.addEventListener("tap", function() {
			aFunc.creatSelectData(0);
		});

		aVariable.btn.endDate.addEventListener("tap", function() {
			aFunc.creatSelectData(1);
		});
		mui('.radio-result').on('change', 'input', function() {
			if (this.checked) {
				switch (this.value) {
					case 'qualified': {
						aVariable.data.ifThis = true;
						aVariable.view.startTime.style.display = "none";
						aVariable.view.endTime.style.display = "none";
					}
					break;
				case 'no-qualified': {
					aVariable.data.ifThis = false;
					aVariable.view.startTime.style.display = "";
					aVariable.view.endTime.style.display = "";
					aVariable.data.startTime = "";
					aVariable.data.endTime = "";
					aVariable.btn.startDate.innerHTML = "请选择开始日期";
					aVariable.btn.endDate.innerHTML = "请选择结束日期";
				}
				break;
				}
			}
		});

		aVariable.btn.confirm_btn.addEventListener("tap", function() {
			var myselect = document.getElementById("reason");
			var index = myselect.selectedIndex;
			var remarkType = myselect.options[index].value;
			var remark = aVariable.view.textarea.value;
			var classId = aVariable.data.classId;
			var signId = aVariable.data.signId;
			var remarkId = aVariable.data.remarkId;
			if (remarkType.length == 0) {
				util.ToastTitle("请选择类型！");
				retun;
			}
			if (remark.length == 0) {
				util.ToastTitle("请说明原因！");
				retun;
			}
			var _this = this;
			if (aVariable.data.ifThis) {
				var wt = plus.nativeUI.showWaiting("提交中...");
				aFunc.requsetConfirm(remarkType, "", "", remark, classId, signId, remarkId, _this, wt);
			} else {
				var beginDate = aVariable.data.startTime;
				var endDate = aVariable.data.endTime;
				if (beginDate.length == 0) {
					util.ToastTitle("请选择开始日期！")
					return;
				}
				if (endDate.length == 0) {
					util.ToastTitle("请选择结束日期！")
					return;
				}
				var wt = plus.nativeUI.showWaiting("提交中...");
				aFunc.requsetConfirm(remarkType, beginDate, endDate, remark, classId, signId, remarkId,
					_this, wt);
			}

		});

	},

	requsetConfirm: function(remarkType, beginDate, endDate, remark, classId, signId, remarkId, _this, wt) {
		mui(_this).button('loading');
		attendServer.remark(remarkType, beginDate, endDate, remark, classId, signId, remarkId, function(data) {
			util.ToastTitle("提交成功！")
			wt.close();
			mui(_this).button('reset');
			if (data.code == "200") {
				var opener = plus.webview.getWebviewById("sign-details");
				mui.fire(opener, 'reloadView');
				setTimeout(function() {
					mui.back();
				}, 1500);

			} else {
				util.ToastTitle(data.msg);
			}
		}, function(error) {
			mui(_this).button('reset');
			wt.close();
			if (error != null) {
				if (JSON.stringify(error).length < 5) {
					util.ToastTitle("提交异常说明失败");
					return;
				}
				util.ToastTitle(JSON.parse(error).msg);
			} else {
				util.ToastTitle("提交异常说明失败");
			}
		});
	},

	requsetRemarkType: function() {
		var selectul = document.getElementById("reason");
		attendServer.remarkType(function(data) {
			if (data.code == "200") {
				if (data.data.length > 0) {
					var optionhtml = aFunc.addOptionHtml(data.data);
					selectul.innerHTML = optionhtml;
					if (aVariable.data.remarkId != null && aVariable.data.remarkId.length > 0) {
						aFunc.requsetRemarkData();
					}
				} else {
					util.ToastTitle("未找到类型");
				}
			} else {
				util.ToastTitle(data.msg);
			}
		}, function(error) {
			if (error != null) {
				if (JSON.stringify(error).length < 5) {
					util.ToastTitle("请求类型失败");
					return;
				}
				util.ToastTitle(JSON.parse(error).msg);
			} else {
				util.ToastTitle("请求类型失败");
			}
		});
	},

	requsetRemarkData: function() {
		attendServer.getRemark(aVariable.data.remarkId, function(data) {
			if (data.code == "200") {
				var deOption = aVariable.view.reason.options[0];
				if (data.data.remarkTypeName != null && data.data.remarkTypeName.length > 0) {
					deOption.innerHTML = data.data.remarkTypeName;
				}
				if (data.data.remarkType != null && data.data.remarkType > 0) {
					deOption.value = data.data.remarkType;
				}
				aVariable.view.textarea.value = data.data.remark;
				if (data.data.beginDate != null && data.data.beginDate.length > 0) {
					aVariable.data.ifThis = false;
					aFunc.selectRadio("day-radio", "no-qualified");
					aVariable.btn.startDate.innerHTML = data.data.beginDate;
					aVariable.btn.endDate.innerHTML = data.data.endDate;
					aVariable.data.startTime = data.data.beginDate;
					aVariable.data.endTime = data.data.endDate;
					aVariable.view.startTime.style.display = "";
					aVariable.view.endTime.style.display = "";
				}
			} else {
				util.ToastTitle(data.msg);
			}
		}, function(error) {
			if (error != null) {
				if (JSON.stringify(error).length < 5) {
					util.ToastTitle("异常说明失败");
					return;
				}
				util.ToastTitle(JSON.parse(error).msg);
			} else {
				util.ToastTitle("异常说明失败");
			}
		});
	},

	addOptionHtml: function(data) {
		var html = '<option id="defaultSel" value="">请选择类型</option>';
		for (var i = 0; i < data.length; i++) {
			var typeData = data[i];
			var key = Object.keys(typeData);
			html += '<option value="' + key + '">' + typeData[key] + '</option>';

		}
		return html;
	},


	// 日期选择器
	creatSelectData: function(dateTime) {
		var dDate = new Date();
		var minDate = new Date();
		minDate.setFullYear(2000, 0, 1);
		var maxDate = new Date();
		maxDate.setFullYear(2100, 11, 31);
		plus.nativeUI.pickDate(function(e) {
			var d = e.date;
			if (dateTime == 0) {
				aVariable.data.startTime = d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d
					.getDate();
				aVariable.btn.startDate.innerHTML = aVariable.data.startTime;

			} else if (dateTime == 1) {
				aVariable.data.endTime = d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate();
				aVariable.btn.endDate.innerHTML = aVariable.data.endTime;
			}
		}, function(e) {}, {
			title: "请选择日期",
			date: dDate,
			minDate: minDate,
			maxDate: maxDate
		});
	},


	selectRadio: function(Name, Value) {
		var tesObj = document.getElementsByName(Name);
		for (var i = 0; i < tesObj.length; i++) {
			if (tesObj[i].value == Value) {
				tesObj[i].checked = true;
				break;
			}
		}
	}



};
